<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends BaseController {

	public function __construct() {
        parent::__construct();
        // 加载库
        $this->load->model('webModel');
    }

    public function index() {
        redirect(admin_url('school/index'));
        $this->tabName = 'index';
        // 获取轮播图列表
        $this->bannerList = $this->webModel->get_banner_list();
        // 获取前4条新闻
        $newsList = $this->webModel->get_news_list(array(
            'pageStart' => 0,
            'pageSize' => 4
        ));
        if($newsList) {
            foreach($newsList as &$news) {
                // 获取更新日期
                $updatetimeArr = explode(' ', $news['updatetime']);
                $updatedate = $updatetimeArr[0];
                $updatedateArr = explode('-', $updatedate);
                $news['updatedate'] = $updatedate;
                $news['updateyear'] = $updatedateArr[0];
                $news['updatemonth'] = $updatedateArr[1];
                $news['updateday'] = $updatedateArr[2];
            }
            // 第一条新闻
            $this->firstNews = $newsList[0];
            array_shift($newsList);
            // 2-4条新闻
            $this->newsList = $newsList;
        }
        // 获取前8条产品
        $params['pageStart'] = 0;
        $params['pageSize'] = 8;
        $productList = $this->webModel->get_product_list($params);
        $this->productList = $productList;
        // 获取产品分类列表
        $this->categoryList = $this->webModel->get_product_category_list();
        // 获取前8条案例
        $this->caseList = $this->webModel->get_case_list(array(
            'pageStart' => 0,
            'pageSize' => 8
        ));
        $this->display('home' . DIRECTORY_SEPARATOR . "index");
    }
    public function about() {
        $this->tabName = 'about';
        $this->display('home' . DIRECTORY_SEPARATOR . "about");
    }
    // 产品列表页面
    public function product() {
        $this->tabName = 'product';
        if($this->get('cate')) {
            $params['cateId'] = $this->cate = $this->get('cate');
        } else {
            $this->cate = 0;
        }
        // 获取产品总数
        $this->count = $this->webModel->get_product_list(array(
            'getRowNumber' => 1,
            'cateId' => $this->get('cate') ? $this->get('cate') : 0
        ));
        $page = $this->get('page') ? $this->get('page') : 1;
        // 当前页数
        $this->page = $page;
        $pageSize = 9;
        // 总共页数
        $this->pageCount = ceil($this->count / $pageSize);
        // var_export($this->count);var_export($this->pageCount);exit;
        // 获取新闻列表
        $pageStart = ($page - 1) * $pageSize;
        $params['pageStart'] = $pageStart;
        $params['pageSize'] = $pageSize;

        $productList = $this->webModel->get_product_list($params);
        $this->productList = $productList;
        // 获取产品分类列表
        $this->categoryList = $this->webModel->get_product_category_list();
        // 获取产品列表
        $this->display('home' . DIRECTORY_SEPARATOR . "product");
    }
    // 产品详情页面
    public function product_detail($id) {
        $this->tabName = 'product';
        $product = $this->webModel->get_product_info($id);
        // 更新时间去掉秒
        $product['updatetime'] = time_remove_second($product['updatetime']);
        // 获取所有列表
        $productList = $this->webModel->get_product_list();
        // 用id作为键值
        // $newsList = array_value_to_key($newsList, 'id');
        foreach($productList as $key => $item) {
            if($item['id'] == $product['id']) {
                if(isset($productList[$key - 1])) {
                    // 上一条
                    $product['last'] = array(
                        'id' => $productList[$key - 1]['id'],
                        'title' => $productList[$key - 1]['title']
                    );
                }
                if(isset($productList[$key + 1])) {
                    // 下一条
                    $product['next'] = array(
                        'id' => $productList[$key + 1]['id'],
                        'title' => $productList[$key + 1]['title']
                    );
                }
            }
        }
        // 获取商品分类
        $productCate = $this->webModel->get_product_category_info($product['cate_id']);
        $product['cateTitle'] = $productCate['title'];
        $this->product = $product;
        // 获取产品分类列表
        $this->categoryList = $this->webModel->get_product_category_list();
        // var_export($product);exit;
        $this->display('home' . DIRECTORY_SEPARATOR . "product_detail");
    }
    // 新闻列表页面
    public function news() {
        $this->tabName = 'news';
        // 获取新闻总数
        $this->count = $this->webModel->get_news_list(array(
            'getRowNumber' => 1
        ));
        $page = $this->get('page') ? $this->get('page') : 1;
        // 当前页数
        $this->page = $page;
        $pageSize = 10;
        // 总共页数
        $this->pageCount = ceil($this->count / $pageSize);
        // var_export($this->count);var_export($this->pageCount);exit;
        // 获取新闻列表
        $pageStart = ($page - 1) * $pageSize;
        $params['pageStart'] = $pageStart;
        $params['pageSize'] = $pageSize;
        $newsList = $this->webModel->get_news_list($params);
        foreach($newsList as &$news) {
            // 获取更新日期
            $updatetimeArr = explode(' ', $news['updatetime']);
            $updatedateArr = explode('-', $updatetimeArr[0]);
            $news['updateyear'] = $updatedateArr[0];
            // 去掉年份
            array_shift($updatedateArr);
            $news['updatedate'] = implode('-', $updatedateArr);
        }
        $this->newsList = $newsList;
        // var_export($this->newsList);exit;
        $this->display('home' . DIRECTORY_SEPARATOR . "news");
    }
    // 新闻详情页面
    public function news_detail($id) {
        $this->tabName = 'news';
        $news = $this->webModel->get_news_info($id);
        // 更新时间去掉秒
        $news['updatetime'] = time_remove_second($news['updatetime']);
        // 获取所有列表
        $newsList = $this->webModel->get_news_list();
        // 用id作为键值
        // $newsList = array_value_to_key($newsList, 'id');
        foreach($newsList as $key => $item) {
            if($item['id'] == $news['id']) {
                if(isset($newsList[$key - 1])) {
                    // 上一条
                    $news['last'] = array(
                        'id' => $newsList[$key - 1]['id'],
                        'title' => $newsList[$key - 1]['title']
                    );
                }
                if(isset($newsList[$key + 1])) {
                    // 下一条
                    $news['next'] = array(
                        'id' => $newsList[$key + 1]['id'],
                        'title' => $newsList[$key + 1]['title']
                    );
                }
            }
        }
        $this->news = $news;
        // var_export($news);exit;
        $this->display('home' . DIRECTORY_SEPARATOR . "news_detail");
    }
    // 案例页面
    public function cases() {
        $this->tabName = 'cases';
        // 获取案例总数
        $this->count = $this->webModel->get_case_list(array(
            'getRowNumber' => 1
        ));
        $page = $this->get('page') ? $this->get('page') : 1;
        // 当前页数
        $this->page = $page;
        $pageSize = 8;
        // 总共页数
        $this->pageCount = ceil($this->count / $pageSize);
        // var_export($this->count);var_export($this->pageCount);exit;
        // 获取案例列表
        $pageStart = ($page - 1) * $pageSize;
        $params['pageStart'] = $pageStart;
        $params['pageSize'] = $pageSize;
        $caseList = $this->webModel->get_case_list($params);
        foreach($caseList as &$case) {
            // 获取更新日期
            $updatetimeArr = explode(' ', $case['updatetime']);
            $updatedateArr = explode('-', $updatetimeArr[0]);
            $case['updateyear'] = $updatedateArr[0];
            // 去掉年份
            array_shift($updatedateArr);
            $case['updatedate'] = implode('-', $updatedateArr);
        }
        $this->caseList = $caseList;
        $this->display('home' . DIRECTORY_SEPARATOR . "cases");
    }
    // 案例详情页面
    public function case_detail($id) {
        $this->tabName = 'cases';
        $case = $this->webModel->get_case_info($id);
        // 更新时间去掉秒
        $case['updatetime'] = time_remove_second($case['updatetime']);
        // 获取所有列表
        $caseList = $this->webModel->get_case_list();
        foreach($caseList as $key => $item) {
            if($item['id'] == $case['id']) {
                if(isset($caseList[$key - 1])) {
                    // 上一条
                    $case['last'] = array(
                        'id' => $caseList[$key - 1]['id'],
                        'title' => $caseList[$key - 1]['title']
                    );
                }
                if(isset($caseList[$key + 1])) {
                    // 下一条
                    $case['next'] = array(
                        'id' => $caseList[$key + 1]['id'],
                        'title' => $caseList[$key + 1]['title']
                    );
                }
            }
        }
        $this->case = $case;
        // var_export($case);exit;
        $this->display('home' . DIRECTORY_SEPARATOR . "case_detail");
    }
    // 联系我们页面
    public function contact() {
        $this->tabName = 'contact';
        $this->display('home' . DIRECTORY_SEPARATOR . "contact");
    }
}